package com.ponxu.mb.web.action.admin;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ponxu.log.Log;
import com.ponxu.log.LogFactory;
import com.ponxu.mb.model.User;
import com.ponxu.mb.service.OptionService;
import com.ponxu.mb.service.UserService;
import com.ponxu.mb.web.action.MiniBlogAction;
import com.ponxu.utils.Lang;
import com.ponxu.web.core.WebContext;

/**
 * 博客设置
 * 
 * @author xwz
 */
public class SettingAction extends MiniBlogAction {
	private static final Log LOG = LogFactory.getLog();
	private static final String FTL_SETTING = "/admin/setting.ftl";

	public String execute(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		LOG.debug("setting..");
		return FTL_SETTING;
	}

	/**
	 * 保存
	 */
	public String save(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		WebContext context = new WebContext(request, response);
		String msg = "保存成功!";

		String blog_name = context.getParameter("blog_name");
		String blog_description = context.getParameter("blog_description");
		String theme = context.getParameter("theme");
		String blog_notice = context.getParameter("blog_notice");

		int userId = context.getIntParameter("userId");
		String name = context.getParameter("name");
		String password = context.getParameter("password");

		OptionService.wirte("blog_name", blog_name);
		OptionService.wirte("blog_description", blog_description);
		OptionService.wirte("theme", theme);
		OptionService.wirte("blog_notice", blog_notice);

		User user = UserService.getById(userId);
		if (Lang.isNotEmpty(name)) {
			user.setName(name);
		}
		if (Lang.isNotEmpty(password)) {
			user.setPassword(Lang.md5(password));
		}
		UserService.update(user);

		context.put("msg", msg);
		return FTL_SETTING;
	}
}